package ModelLayer;
import java.util.ArrayList;
// used for getting the system date
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
 * Contains the information to create a sale
 * 
 * @author (gruppe 3)
 */

public class Sale
{       
   private Customer customer;
   private Employee employee;
   
   private double totalPrice;
   private String date;
   
   // number unique for each sale, add 1 up for each sale
   private static int invoiceNumber;
   private int saleInvoiceNumber;
   
   // collections
   public ArrayList<PartSale> partSales;
   
   private static Sale instance;
   
  /**
   * constructor
   */
   private Sale()
   {   
        this.customer = null;
        this.employee = null;
        this.totalPrice = 0;      
        this.date = actualDate();
        this.invoiceNumber = invoiceNumber + 1;
        this.saleInvoiceNumber = invoiceNumber;
       
        partSales = new ArrayList<PartSale>();
   }
   
   /**
    * singleton
    */
   public static Sale getInstance()
   {
       if(instance == null)
       {
            instance = new Sale();
       }
       return instance;
   }
   
   /**
    * sets the single instance equal to null
    */
   public static void removeInstance() 
   {
       instance = null;
   }
   
    // get / set methodes
   public void setTotalPrice(double totalPrice)
   {
        this.totalPrice = totalPrice;
   }
    
   public double getTotalPrice()
   {
        return totalPrice;
   }
    
   public void setDate(String date)
   {
        this.date = actualDate();
   }
    
   public String getDate()
   {
        return date;
   }  
   
   public void setCustomer(Customer customer)
   {
       this.customer = customer;
   }
   
   public Customer getCustomer()
   {
       return customer;
   }
   
   public void setEmployee(Employee employee)
   {
       this.employee = employee;
   }
   
   public Employee getEmployee()
   {
       return employee;
   }
   
   // only a get method for the sale invoice number, no set method
   public int getSaleInvoiceNumber()
   {
       return saleInvoiceNumber;
   }
   

   
   /**
    * Adds a new item to the ArrayList partSales 
    * @param newSaleItem  Item to be added to the sale
    * @param amount       amount of the item to be added
    */
   public void addSaleItem(Item newSaleItem, int amount)
   {    
        PartSale newPartSale = new PartSale(newSaleItem, amount);
        if (newPartSale != null && amount >= 0)
        {   
            // add the new item and amount to the salelist
            partSales.add(newPartSale);
            
            // variable to contain total price of the item and its amount
            double TotalAddedItemPrice = newPartSale.getItem().getPrice() * newPartSale.getAmount();
            double newTotalPrice = getTotalPrice() +  TotalAddedItemPrice;
            setTotalPrice(newTotalPrice);
            System.out.println(amount + " stk. af " + newSaleItem.getName() + " blev tilføjet salget");
        }
   }
   
   /** 
    * Removes an item from partSales
    * @param saleItemCode id / code of the item to be removed from the salelist
    */
   public Item removeSaleItem(String saleItemCode)
   {    
        if (saleItemCode != null)
        {
            for (int x = 0; x < partSales.size(); x++)
            {   
                if (partSales.get(x).getItem().getItemCode().equalsIgnoreCase(saleItemCode))
                {   
                    // calculates a new total price
                    double TotalRemovedItemPrice = partSales.get(x).getItem().getPrice() * partSales.get(x).getAmount();
                    double newTotalPrice = getTotalPrice() -  TotalRemovedItemPrice;
                    setTotalPrice(newTotalPrice);
                    
                    // removes the item from the salelist
                    System.out.println(partSales.get(x).getAmount() + " stk. af " + 
                                       partSales.get(x).getItem().getName() + " blev fjernet fra salget");
                    partSales.remove(x);  
                    
                }
            }
        }
        return null;
   }
   
   /**  
    *  Finds an employe and adds the employee to the sale
    *  @param saleEmployee employee object to be added to the sale
    */
   public void getEmployee(Employee saleEmployee)
   {
       if (saleEmployee != null)
       {
           setEmployee(saleEmployee);
       }
   }
   
   /**
    * Finds a customer and adds the customer to the sale
    * @param saleCustomer customer object to be added to the sale
    */
   public void getCustomer(Customer saleCustomer)
   {
       if (saleCustomer != null)
       {
           setCustomer(saleCustomer);
       }
   }
   
   /**
    * Ends the sale and calculate a the final price
    * @return total price, if it is bigger than zero, otherwise returns 0.0
    */
   public double endSale()
   {   
       System.out.println("Den samlet pris er: " + totalPrice);  
       
       if (getTotalPrice() > 0.0)
       {
           return getTotalPrice();
       }
       
       return 0.0;
   }
   
   /**   
    * Prints the invoice for the sale
    */
   public void printInvoice()
   {   
       System.out.println("Vestbjerg Byggecenter A/S "); 
       System.out.println("");
       
       if (customer != null)
       {    
           System.out.println();
           System.out.println("Kunde: " + customer.getName());
           System.out.println("Kundens rabat: " + (customer.getDiscount() * 100) + "%");
           System.out.println();
       }
       
       if (partSales != null)
       {
           for (int x = 0; x < partSales.size(); x++)
           {    
               double buyPrice = 0;
               buyPrice = partSales.get(x).getItem().getPrice() * partSales.get(x).getAmount();
               System.out.println(partSales.get(x).getAmount() + " stk. " + partSales.get(x).getItem().getName() + 
               "(code: " + partSales.get(x).getItem().getItemCode() + ") a." + partSales.get(x).getItem().getPrice() 
               + " - samlet ialt: " + buyPrice);             
           }
       }
       
       System.out.println("Dato: " + actualDate());
       System.out.println("Invoice nummer: " + this.getSaleInvoiceNumber());
       if (employee != null)
       {    
           System.out.println();
           System.out.println("Du blev betjent af: " + employee.getName());
           System.out.println();
       }
   }
   
   /**
    *  lists all partsales in ArrayList partSales 
    */   
   public void listAllPartSales()
   {         
        if (partSales != null)
        {               
            for (int x = 0; x < partSales.size(); x++)
            {
                System.out.println(partSales.get(x).getAmount() + " stk. af " + partSales.get(x).getItem().getName() + " - " +
                                   partSales.get(x).getItem().getPrice() + " per stk. I alt: " +
                                   partSales.get(x).getItem().getPrice() * partSales.get(x).getAmount());
            }
        }
   }
   
   /**
     * Get the actual system date and time when it is called
     * @return actualDate, which contain a string of the date / time, when it was called
     */
   private String actualDate()
   {
        DateFormat dateFormat = new SimpleDateFormat("EEEEEEE d MMM yyyy HH:mm");
        Date date = new Date();
        String actualDate = dateFormat.format(date);
        return actualDate;
   }
}
